package com.dces.jx.billtest.dataimport;

import com.dces.jx.billtest.dataimport.base.AbstractTempTabImportHandler;
import com.dces.jx.billtest.dataimport.model.ListTemp;
import com.dces.jx.billtest.userAdd.dao.DcBillListZhjMapper;
import com.dces.jx.billtest.util.MapDataUtil;
import com.xdo.dataimport.utils.XdoImportLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.dces.jx.billtest.dataimport.constant.Constants.*;

@Service
@Slf4j
public class ListImportHandler extends AbstractTempTabImportHandler<ListTemp> {
    @Resource
    private DcBillListZhjMapper dcBillListZhjMapper;
    @Override
    /**
     * 方法二(xxl第一次调用该任务时，进入本方法校验临时表数据过程)
     * 临时表校验数据过程
     * @param correctList 正确数据集合
     * @param wrongList 错误数据集合
     * @param warnList 警告数据集合
     * @param mapBasicParam 基础业务参数
     * @param mapBusinessParam 账册编号等业务参数
     * @param tempOwnerId 导入批次号
     */
    protected void checkTempData(List<ListTemp> correctList, List<ListTemp> wrongList, List<ListTemp> warnList, Map<String, Object> mapBasicParam, Map<String, Object> mapBusinessParam, String tempOwnerId) {
        String headId = MapDataUtil.getMapStr(mapBusinessParam, "headId");
        // 校验导入临时表的数据
        dcBillListZhjMapper.checkData(tempOwnerId);
        // 校验Excel导入的数据中心中是否存在重复
        dcBillListZhjMapper.checkPrimaryKey(tempOwnerId);
        // 查询校验正确数据, 错误数据
        correctList.addAll(dcBillListZhjMapper.selectCheckDataByTempFlag(tempOwnerId, CORRECT_FLAG));
        wrongList.addAll(dcBillListZhjMapper.selectCheckDataByTempFlag(tempOwnerId, WRONG_FLAG));
        XdoImportLogger.log("correct：%d;wrong：%d;warn:%d", correctList.size(), wrongList.size(), warnList.size());
    }

    @Override
    protected void insertOrUpdateTempData(String tempOwnerId, Map<String, Object> mapBasicParam, Map<String, Object> mapBusinessParam) {
        String headId = MapDataUtil.getMapStr(mapBusinessParam, "headId");
        String insertUser = MapDataUtil.getMapStr(mapBusinessParam, "insertUser");
        // (修改导入时先更新数据，为了防止更新冗余数据)更新数据(业务根据需求自己实现)
        int updateNum = dcBillListZhjMapper.updateData(headId, tempOwnerId, insertUser);
        // 插入新增数据(业务根据需求自己实现)
        int insertNum = dcBillListZhjMapper.insertData(headId, tempOwnerId, insertUser);
        XdoImportLogger.log("insert：%d;update：%d;", insertNum, updateNum);
    }

    @Override
    public String getTaskCode() {
        return BILL_HEAD;
    }
}
